package com.wishwall.utils;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.wishwall.beans.basic.ErrorBean;
import com.wishwall.beans.basic.StatusBean;

public class ServiceUtils {

	/**
	 * 
	 * @param code
	 * @param message
	 * @return
	 */
	public static Response getErrorResponse(int code, Exception e) {
		e.printStackTrace();
		
		ErrorBean errorBean = new ErrorBean();
		errorBean.setErrorCode(code);//I don't know whether they must be the same as the following one
		errorBean.setMessage(e.getMessage());
		return Response.status(code).entity(errorBean).type(MediaType.APPLICATION_JSON).build();
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	public static String getBaseURL(javax.servlet.http.HttpServletRequest request) {

		StringBuilder url = new StringBuilder();
		url.append(request.getScheme()).append("://");
		url.append(request.getServerName());
		int serverPort = request.getServerPort();
		if (serverPort != 80) {
			url.append(":").append(serverPort);
		}
		url.append(request.getContextPath());
		return url.toString();
	}

	/**
	 * 
	 * @param id
	 * @param downloadUrl
	 * @param request
	 * @return
	 */
	public static StatusBean getStatusBean(Object id, String downloadUrl, javax.servlet.http.HttpServletRequest request) {

		String hostname = getBaseURL(request);
		StatusBean statusBean = new StatusBean();
		statusBean.setStatusCode(Response.Status.OK.getStatusCode());
		statusBean.setDownloadURL(hostname + downloadUrl);
		statusBean.setId(id);

		return statusBean;
	}

	/**
	 * 
	 * @param id
	 * @param message
	 * @return
	 */
	public static StatusBean getStatusBean(Object id, String message) {

		StatusBean statusBean = new StatusBean();
		statusBean.setStatusCode(Response.Status.OK.getStatusCode());
		statusBean.setMessage(message);
		statusBean.setId(id);

		return statusBean;
	}

}
